﻿using ASmile.Extend;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmInput : FrmBaseModal
    {
        public FrmInput(Func<string, ApiResult> SaveHandle, string defStr = null)
        {
            InitializeComponent();
            OnSave = SaveHandle;
            DefText = defStr;
        }

        Func<string, ApiResult> OnSave;
        string DefText = null;

        private void FrmInput_Load(object sender, EventArgs e)
        {
            if (DefText.IsNotEmpty())
            {
                txtInput.Text = DefText;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var r = OnSave(txtInput.Text);
            if (!r)
            {
                ShowMessage(r.msg);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK.Focus();
            }
        }
    }
}
